//
//  SearchDetailViewController.m
//  礼物
//
//  Created by dllo on 15/11/9.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "SearchDetailViewController.h"
#import "SearchGiftCollectionViewCell.h"
#import "CategoryDetailModel.h"
#import "CategoryModel.h"
#import "EveryDetailViewController.h"

@interface SearchDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)BaseCollectionView *collectionV;
@property (nonatomic, retain) NSMutableArray *dataArray;//model数组

@property (nonatomic, assign) NSInteger currentPage;//记录当前page
@property (nonatomic, assign) BOOL isUpLoading;//标记是否为上拉

@end

@implementation SearchDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray array];
    UILabel *titleLabels = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 4, 44)];
    titleLabels.text = self.category.name;
    titleLabels.textColor = [UIColor whiteColor];
    titleLabels.textAlignment = NSTextAlignmentCenter;
    titleLabels.font = [UIFont fontWithName:@"ArialHebrew-Bold" size:20];
    self.navigationItem.titleView = titleLabels;

    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((WIDTH - 30) / 2, (HEIGHT - 113) / 2);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 10;
    //设置item的列间距
    flowLayout.minimumInteritemSpacing = 10;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //collectionView
    self.collectionV = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    self.collectionV.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionV];
    //注册cell
    [self.collectionV registerClass:[SearchGiftCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self addHeader];
    [self addFooter];
    // Do any additional setup after loading the view.
}
-(void)addHeader
{
    self.collectionV.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        self.currentPage = 0;
        [self getAFN:self.currentPage andId:self.category.category_id];
        
    }];
    // 马上进入刷新状态
    [self.collectionV.header beginRefreshing];

}
-(void) addFooter
{
   self.collectionV.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
       self.isUpLoading = YES;
       self.currentPage += 20;
       [self getAFN:self.currentPage andId:self.category.category_id];
    }];
}
-(void)getAFN:(NSInteger) page andId:(NSString *)categry_id
{
    NSString *url_string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=%ld",categry_id,page];
    NSLog(@"=======url = %@",url_string);
    [AppToolss getAFNetWorkingData:url_string andBlock:^(id data) {
        if (!self.isUpLoading) {
            [self.dataArray removeAllObjects];
        }
        NSMutableDictionary *dictionary = data;
        NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"items"];
        
        for (NSMutableDictionary *dic in array)
        {
            CategoryDetailModel *cateDetail = [[CategoryDetailModel alloc] init];
            [cateDetail setValuesForKeysWithDictionary:dic];
            cateDetail.descriptions = [dic objectForKey:@"description"];
            
            [self.dataArray addObject:cateDetail];
            [self.collectionV.header endRefreshing];
            [self.collectionV.footer endRefreshing ];
            [self.collectionV reloadData];
//            NSLog(@"dataArray = %@",self.dataArray);
        }

        
    } andError:^(id failure) {
         [self.collectionV.header endRefreshing];
        
    }];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryDetailModel *categoryDetailModel = [self.dataArray objectAtIndex:indexPath.row];
    EveryDetailViewController *everyDetailVC = [[EveryDetailViewController alloc] init];
    everyDetailVC.categoryDetail = categoryDetailModel;
    [self.navigationController pushViewController:everyDetailVC animated:YES];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    cell.layer.cornerRadius = 5;//设置圆角度
    
    CategoryDetailModel *categoryDetailModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.cateDetail = categoryDetailModel;
    

    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
